<?php
namespace Admin\Model;
use Think\Model;
/**
 * 个人日程管理
 * 
 */

class EventModel extends Model{
    public function indexs(){
        $uinfo = UserModel::loadFromSession();
        return $uinfo;
    }
	//添加日程接口
	public function add_event( $title, $content, $location, $owner, $owner_id, $owner_sector, $owner_role, $starttime, $endtime )
	{
        $uinfo = UserModel::loadFromSession();
		$r = array('d'=>NULL,'i'=>'','r'=>0);
        if( !$uinfo )
            $r['i'] = '加载用户信息失败';
        else{
            $d = array();
            $d['title'] = $title;
            $d['content'] = $content;
            $d['location'] = $location;
            $d['create_time'] = date('Y-m-d H:i:s');
            $d['update_time'] = $d['create_time'];
            $d['starttime'] = $starttime;
            $d['endtime'] = $endtime;
            if($owner_id){
              $d['owner_id'] = $owner_id;
              $d['owner_sector'] = $owner_sector;
              $d['owner_role'] = $owner_role;
            }
            else
                $d['owner_id'] = '';
            $d['creator_sector'] =  $uinfo['sector_id'];
            $d['creator_id'] = $uinfo['user_id'];
            $d['creator_role'] = $uinfo['role_id'];
            $d['is_deleted'] = 0;
            $addID = $this->add( $d );
            if( $addID ){
                $r['r'] = $addID ;
                $r['i'] = 'OK';
                $r['d'] = $this->where( 'event_id='.$addID )->find();
                $r['owner'] = $owner;
            }else{
                $r['i'] = '保存失败';
            }
        }

		return $r;
	}
    //删除日程
    public function delete_event( $event_id ){
        $r = array('r'=>0,'i'=>'server error','d'=>NULL );
        if( $event_id )
            $r['i'] = '参数错误！';
        $wheres = array();
        $wheres['event_id'] = array('eq', $event_id);
        $wheres['is_deleted'] = array('neq', 1);
        $find = $this->where( $wheres )->find();
        if( $find ){
            $d = array();
            $d['is_deleted'] = 1;
            $delID = $this->where( $wheres )->save( $d );
            if(  $delID ){
                $r['r'] = 1;
                $r['i'] = 'OK';
            }else{
                $r['i'] = '日程删除失败！';
            }
        }else
            $r['i'] = '抱歉，数据不存在！';
        return $r;
    }
    //更新日程
    public function update_event($event_id, $title, $content, $location, $owner, $owner_id, $starttime, $endtime )
    {
        $r = array('d'=>NULL,'i'=>'','r'=>0);
        if( strtotime($starttime) > strtotime($endtime) ){
            $r['i'] =  '日程结束日期不能大于开始时间';
            return $r;
        }
        if( $event_id && strlen(htmlspecialchars($content)) ) {
            $devent = null;
            $wheres = array();
            $wheres['event_id'] = array('eq', $event_id );
            $wheres['is_deleted'] = array('neq', 1);
            $devent = $this->where( $wheres )->find();
            if( $devent ){
                $d = array();
                $d['update_time'] = date('Y-m-d H:i:s');
                $d['title'] = $title;
                $d['content'] = $content;
                $d['location'] = $location;
                $d['owner_id'] = $owner_id;
                $d['starttime'] = $starttime;
                $d['endtime'] = $endtime;
                $deitID = $this->where( $wheres )->save( $d );
                if( $deitID ){
                    $r['r'] = 1;
                    $r['d'] = $this->where( $wheres )->find();
                    $r['i'] = 'OK';
                } else{
                    $r['i'] = '日程更新失败！';
                }
            } else
                $r['i'] = '抱歉，数据不存在！';
        } else{
            $r['i'] = '参数错误！';
        }

        return $r;
    }
   //获取本部门、下属部门信息
    public function getUinfo(){
       return $uinfo = UserModel::loadFromSession();
    }
/* 
      //查找下属部门
    public function getchild($role_id){
        $role =  M('role');
        $data = $role->select();
        return $this->getchildids($data,$role_id);
   }

   public function getchildids($data,$roleid){
      static $arrid=array();
      $arrid[]=$roleid;
      foreach ($data as $key => $value) {
         if ($value['parent_role'] == $roleid) {//找子类
            $arrid[]=$value['role_id'];
            $this->getchildids($data,$value['role_id']);
         }
      }
      return array_unique($arrid);
   }

   //查找下属部门
    public function getchild_sector($sector_id){
        $sector =  M('sector');
        $data = $sector->select();
        return $this->getchildids_sector($data,$sector_id);
   }

   public function getchildids_sector($data,$sectorid){
      static $arrid=array();
      $arrid[]=$sectorid;
      foreach ($data as $key => $value) {
         if ($value['parent_sector'] == $sectorid) {//找子类
            $arrid[]=$value['sector_id'];
            $this->getchildids_sector($data,$value['sector_id']);
         }
      }
      return array_unique($arrid);
   }*/


   //获取本周起止时间
    function getWeek(){
      $sdefaultDate = date("Y-m-d");
      $first = 1;    
      $w = date('w',strtotime($sdefaultDate));  
      $week_start = date('Ymd',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));  
      $week_end = date('Ymd',strtotime("$week_start +6 days"));
      return array($week_start,$week_end);
    }

}
?>